package cn.agiledata.bank.wap.action;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.service.ICommonService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.wap.common.Message;
import cn.agiledata.bank.wap.form.CommonForm;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class WapBaseAction extends IPBBaseAction implements Constant {
	public ICommonService getCommonService() {
		ICommonService service = null;

			service = (ICommonService)IPBAppFactory.getObj(ICommonService.SERVICE_NAME);

		return service;
	}
	public int hasAccessPermission(int accessLevel, HttpServletRequest request) throws IPBException {
		// TODO Auto-generated method stub
		return 1;
	}
	
	public ActionForward messageProcess(String code,String forwardName,ActionForm form,ActionMapping mapping,HttpServletRequest request) {
		String message = null;
		try {
			Field field = Message.class.getDeclaredField(code);
			message = (String)field.get(Message.class);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			//Message里没有定义的动态出错信息
			message = code;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return messageForward(message,forwardName,form,mapping,request);

	}
	
	public ActionForward messageForward(String message,String forwardName,ActionForm form,ActionMapping mapping,HttpServletRequest request) {
		String subAccountId = "";
		String accountId = "";
		if(form != null) {
			if(form instanceof CommonForm) {
				try {
					subAccountId = (String)PropertyUtils.getSimpleProperty(form, "subAccountId");
					accountId = (String)PropertyUtils.getSimpleProperty(form, "accountId");
					ICommonService service = getCommonService();
					if(!StringUtil.isStrEmpty(accountId)) {
						request.setAttribute("accountId",accountId);
					}
					if(!StringUtil.isStrEmpty(subAccountId)) {
						SubAccount subAccount = service.getSubAccount(subAccountId);
						request.setAttribute("subAccountId",subAccountId);
						request.setAttribute("subAccountBalance",subAccount.getAccountBalance().getBalance().toString());
					}
					
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		request.setAttribute("message",message);
		request.setAttribute("path",request.getContextPath() + mapping.getModuleConfig().findForwardConfig(forwardName).getPath());
		request.setAttribute("goto",forwardName);		
		return mapping.findForward(MESSAGE);		
	}
}
